package cn.cstech.aijia.ui.base.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * Created with Android Studio.
 * Title:SPUtils
 * Description:
 * Copyright:Copyright (c) 2014
 * Company:中国平安健康保险有限公司
 * Author:Alice
 * Date:15/8/24
 * Time:下午3:41
 * Version 1.0
 */
public class SPUtils {
    //保存登录状态的文件名
    public static final String TYPE_LOG_STATUS = "log_status";

    //app版本号
    public static final String APP_LATEST_VER = "app_latest_ver";

    //跳转页面索引
    public static final String EXTRA_MAIN_PAGE_INDEX = "extra_main_page_index";

    //当前所在的城市
    public static final String EXTRA_CURRENT_CITY = "city";
    //当前的地址
    public static final String EXTRA_CURRENT_ADDRESS = "address";
    //当前的Lat
    public static final String EXTRA_CURRENT_LAT = "lat";
    //当前的LNG
    public static final String EXTRA_CURRENT_LNG = "lng";

    public static String EXTRA_CODE = "code";

    public static String EXTRA_MESSAGE = "message";
    //手机号码
    private static final String KEY_MOBILE_PHONE = "mobile_phone";
    //密码
    private static final String KEY_PWD = "pwd";
    //uid
    private static final String KEY_UID = "uid";
    //分享对话框
    public static final String EXTRA_DLG_TITLE = "dlg_title";
    public static final String EXTRA_DLG_MESSAGE = "dlg_message";
    public static final String EXTRA_SHARE_TITLE = "share_title";
    public static final String EXTRA_SHARE_CONTENT = "share_content";
    public static final String EXTRA_SHARE_IMAGE_URL = "share_image_url";
    public static final String EXTRA_SHARE_IMAGE_LOCAL = "share_image_local";
    public static final String EXTRA_SHARE_WEB_PAGE = "share_web_page";
    public static final String EXTRA_SHARE_WEB_PAGE_THUMB = "share_web_page_thumb";
    public static final String EXTRA_SHARE_ACTION = "share_action";
    public static final String ACTION_SHARE_RESULT = "action_share_result";

    public static void save(Context c, String key, String value) {
        save(TYPE_LOG_STATUS, c, key, value);
    }

    public static void save(Context c, String key, boolean value) {
        save(TYPE_LOG_STATUS, c, key, value);
    }

    public static void save(Context c, String key, int value) {
        save(TYPE_LOG_STATUS, c, key, value);
    }

    public static void save(Context c, String key, long value) {
        save(TYPE_LOG_STATUS, c, key, value);
    }

    public static void save(Context c, String key, float value) {
        save(TYPE_LOG_STATUS, c, key, value);
    }

    public static void save(String type, Context c, String key, String value) {
        try {

            if (key != null) {
                SharedPreferences mPreferences = c
                        .getSharedPreferences(type, 0);
                SharedPreferences.Editor mEditor = mPreferences.edit();
                if (value != null) {
                    mEditor.putString(key, value);
                } else {
                    mEditor.remove(key);
                }
                mEditor.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void save(String type, Context c, String key, int value) {
        try {
            SharedPreferences mPreferences = c.getSharedPreferences(type, 0);
            SharedPreferences.Editor mEditor = mPreferences.edit();
            if (key != null) {
                mEditor.putInt(key, value);
            }
            mEditor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void save(String type, Context c, String key, boolean value) {
        try {
            SharedPreferences mPreferences = c.getSharedPreferences(type, 0);
            SharedPreferences.Editor mEditor = mPreferences.edit();
            if (key != null) {
                mEditor.putBoolean(key, value);
            }
            mEditor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void save(String type, Context c, String key, long value) {
        try {
            SharedPreferences mPreferences = c.getSharedPreferences(type, 0);
            SharedPreferences.Editor mEditor = mPreferences.edit();
            if (key != null) {
                mEditor.putLong(key, value);
            }
            mEditor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void save(String type, Context c, String key, float value) {
        try {
            SharedPreferences mPreferences = c.getSharedPreferences(type, 0);
            SharedPreferences.Editor mEditor = mPreferences.edit();
            if (key != null) {
                mEditor.putFloat(key, value);
            }
            mEditor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getString(Context c, String key) {
        return getString(TYPE_LOG_STATUS, c, key);
    }

    public static String getString(String type, Context c, String key) {
        String value = null;
        SharedPreferences preferences = c.getSharedPreferences(type, 0);
        if (preferences == null) {
            return value;
        }
        value = preferences.getString(key, null);
        return value;
    }

    public static long getLong(Context c, String key) {
        return getLong(TYPE_LOG_STATUS, c, key);
    }

    public static long getLong(String type, Context c, String key) {
        long value = 0;
        SharedPreferences preferences = c.getSharedPreferences(type, 0);
        if (preferences == null) {
            return value;
        }

        value = preferences.getLong(key, 0);
        return value;
    }

    public static int getInt(Context c, String key) {
        return getInt(TYPE_LOG_STATUS, c, key);
    }

    public static int getInt(String type, Context c, String key) {
        int value = 0;
        SharedPreferences preferences = c.getSharedPreferences(type, 0);
        if (preferences == null) {
            return value;
        }

        value = preferences.getInt(key, 0);
        return value;
    }

    public static boolean getBoolean(Context c, String key) {
        return getBoolean(TYPE_LOG_STATUS, c, key);
    }

    public static boolean getBoolean(String type, Context c, String key) {
        Boolean value = false;
        SharedPreferences preferences = c.getSharedPreferences(type, 0);
        if (preferences == null) {
            return false;
        }
        value = preferences.getBoolean(key, false);
        return value;
    }

    public static boolean getBoolean(String type, Context c, String key,
                                     boolean defaultValue) {
        Boolean value = false;
        SharedPreferences preferences = c.getSharedPreferences(type, 0);
        if (preferences == null) {
            return false;
        }
        value = preferences.getBoolean(key, defaultValue);
        return value;
    }

    public static float getFloat(Context c, String key) {
        return getFloat(TYPE_LOG_STATUS, c, key);
    }

    public static float getFloat(String type, Context c, String key) {
        float value = 0f;
        SharedPreferences preferences = c.getSharedPreferences(type, 0);
        if (preferences == null) {
            return value;
        }
        value = preferences.getFloat(key, 0f);
        return value;
    }

    public static boolean contains(Context context, String type, String key) {
        SharedPreferences preferences = context.getSharedPreferences(type, 0);
        return preferences.contains(key);
    }

    public static boolean contains(Context context, String key) {
        return contains(context, TYPE_LOG_STATUS, key);
    }

    public static void remove(Context context, String key) {
        try {
            SharedPreferences mPreferences = context.getSharedPreferences(
                    TYPE_LOG_STATUS, 0);
            SharedPreferences.Editor mEditor = mPreferences.edit();
            if (key != null) {
                mEditor.remove(key);
            }
            mEditor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void remove(String type, Context c, String key) {
        try {
            SharedPreferences mPreferences = c.getSharedPreferences(type, 0);
            SharedPreferences.Editor mEditor = mPreferences.edit();
            if (key != null) {
                mEditor.remove(key);
            }
            mEditor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //保存当前的位置信息
    public static void setLocation(Context context,String address,String city,float lat,float lng){
        if(address != null) {
            save(TYPE_LOG_STATUS, context, EXTRA_CURRENT_ADDRESS, address);
        }
        if(city != null) {
            save(TYPE_LOG_STATUS, context, EXTRA_CURRENT_CITY, city);
        }
        if(lat != 0) {
            save(TYPE_LOG_STATUS, context, EXTRA_CURRENT_LAT, lat);
        }
        if(lng != 0) {
            save(TYPE_LOG_STATUS, context, EXTRA_CURRENT_LNG, lng);
        }
    }
    //获取当前的城市信息
    public static String getExtraCurrentCity(Context context){
        return getString(TYPE_LOG_STATUS,context,EXTRA_CURRENT_CITY);
    }
    //获取当前的位置
    public static String getExtraCurrentAddress(Context context){
        return getString(TYPE_LOG_STATUS,context,EXTRA_CURRENT_ADDRESS);
    }
    //获取当前的Lat
    public static String getExtraCurrentLat(Context context){
        return getString(TYPE_LOG_STATUS,context,EXTRA_CURRENT_LAT);
    }
    //获取当前的LNG
    public static String getExtraCurrentLon(Context context){
        return getString(TYPE_LOG_STATUS,context,EXTRA_CURRENT_LNG);
    }
    //设置手机号码
    public static void setMobilePhone(Context context,String phone){
        if(!TextUtils.isEmpty(phone)){
            save(TYPE_LOG_STATUS,context, KEY_MOBILE_PHONE, phone);
        }
    }
    //获取手机号码
    public static String getMobilePhone(Context context){
        return getString(TYPE_LOG_STATUS,context, KEY_MOBILE_PHONE);
    }

}
